import { RouterConfig } from "./RouterConfig";
/**
 * 默认路由
 */
export class RouterBase {
  /**
   * 通过路由名称、图标及标题构造一个页面路由
   * @param {string} name 路由名称
   * @param {string} icon 路由图标
   * @param {string} title 路由标题
   */
  constructor(name, icon = "", title = "") {
    this._name = name;
    if (arguments.length === 1) {
      this._title = name;
    } else if (arguments.length === 2) {
      this._title = icon;
    } else {
      this._icon = icon;
      this._title = title;
    }
    this.path('');
    this._keepAlive = true;
  }

  /**
   * 设置路由排序索引，默认为100
   * @param {number} index - 排序索引
   */
  sort(index) {
    this._sort = index;
    return this;
  }

  /**
   * 设置路由为无缓存页面
   */
  disableCache() {
    this._keepAlive = false;
    return this;
  }

  /**
   * 设置路由地址
   * @param {string} path 路由地址
   */
  path(path) {
    this._path = path;
    return this;
  }

  /**
   * 设置路由重定向内容
   * @param {string} redirect 重定向内容
   */
  redirect(redirect) {
    this._redirect = redirect;
    return this;
  }

  /**
   * 设置组件实例
   * @param {object} component 组件实例
   */
  component(component) {
    this._component = component;
    return this;
  }

  /**
   * 左侧菜单不会显示该页面选项
   */
  hideInMenu() {
    this._hideInMenu = true;
    return this;
  }

  /**
   * 此级路由将不会出现在面包屑中
   */
  hideInBread() {
    this._hideInBread = true;
    return this;
  }

  /**
   * 设置本级路由不需要权限控制
   */
  disablePermission() {
    this._disablePermission = true;
    return this;
  }

  /**
   * 设置路由图标
   * @param {string} icon 图标
   */
  icon(icon) {
    this._icon = icon;
    return this;
  }

  /**
   * 设置后，菜单将在新窗口中打开链接地址（URL）
   * @param {string} href 连接地址
   */
  href(href) {
    this._href = href;
    return this;
  }

  /**
   * 路由标题
   * @param {string} title 标题
   */
  title(title) {
    this._title = title;
    return this;
  }

  /**
   * 设置功能权限清单
   * @param {String[]} powers 权限清单
   */
  powers(powers) {
    this._powers = powers;
    return this;
  }

  /**
   * 在已有权限基础上追加内容
   * @param {String[]} powers 追加的权限清单
   */
  appendPower(powers) {
    this._powers.push(...powers);
    return this;
  }

  /**
   * 设置路由是否为空组件
   */
  isNullComponent() {
    this._isNullComponent = true;
    return this;
  }

  /**
   * 设置路由是为路由组
   */
  isGroup() {
    this._isGroup = true;
    return this;
  }
  /**
   * 指定路由在新窗口中打开
   */
  isNewWindow() {
    this._isNewWindow = true;
    return this;
  }

  /**
   * 在现有自路由清单中追加一个新路由
   * @param {any} route 子路由对象
   */
  append(route) {
    if (!this._children) {
      this._children = [];
    }
    this._children.push(route);
    return this;
  }

  packageProperty() {
    let route = {
      path: this._path || '',
      name: this._name,
      meta: {
        keepAlive: this._keepAlive || false,
        disablePermission: this._disablePermission || false,
        hideInMenu: this._hideInMenu || false,
        hideInBread: this._hideInBread || false,
        extra: this._extra || {},
        title: this._title,
        icon: this._icon,
        href: this._href,
        sort: this._sort ?? 100,
        isGroup: this._isGroup || false,
        isNewWindow: this._isNewWindow || false
      }
    };
    if (this._isNullComponent) {
      route.component = RouterConfig.noneComponent;
    } else {
      route.component = this._component || RouterConfig.defaultComponent;
    }
    if (this._redirect) {
      route.redirect = this._redirect;
    }
    if (this._powers) {
      route.meta.powers = this._powers;
    }
    return route;
  }

  extra(extra) {
    this._extra = extra;
    return this;
  }

  build() { return {} }
}
